import {
    asyncRoutes,
    constantRoutes
} from '@/router'
import Layout from '@/layout'
// import { getResTree01 } from "@/api/use";
import {
    getResTree01
} from '@/api/user'
import { FormItem } from 'element-ui'
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */

function hasPermission(roles, route) {
    // if (route.meta && route.meta.roles) {
    //     /* 路由中存在角色 */
    //     if (roles.some(role => route.meta.roles.includes(role))) {
    //         return true
    //     } else {
    //         return false
    //     }
    // }
    if (route.meta) {
        return true
    }
     else {
        /* 路由中无角色 */
        return false
    }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
    const res = []
    routes.forEach(route => {
        const tmp = {
            ...route
        }
        // if (hasPermission(roles, tmp)) {
        //     // if (tmp.children) {
        //     /* 验证子路由角色权限*/
        //     //     tmp.children = filterAsyncRoutes(tmp.children, roles)
        //     // }
        //     res.push(tmp);
        // }
        if (hasPermission(roles, tmp)) {
            const component = tmp.component;            
            // if(route.component){
            //   if(component == 'Layout'){
            //     tmp.component = Layout;
            //   }else{
            //     tmp.component = (resolve) => require([`@/views${component}`],resolve)
            //   }
            // }
            if(tmp.res_isvalid == false){
                tmp.hidden = true 
            }
            if(tmp.children.length == 0) {
                let newPath = tmp.path.split("/")
                tmp.path = '/' + newPath[0]
                tmp.component = Layout; 
                tmp.redirect = '/' + newPath[0] + '/' + newPath[1] 
                let rewriteChildren = []
                rewriteChildren.push({path: newPath[1], 
                    component: (resolve) => require([`@/views${component}`],resolve),
                    name: '/' + newPath[0] + '/' + newPath[1],
                    meta: tmp.meta
                })
                tmp.children = rewriteChildren
            } else {
                if(tmp.path == ['#']){ 
                    let redirect = tmp.children[0].path
                    tmp.redirect = '/' + redirect
                    tmp.name = redirect.split("/")[0]
                    tmp.component = Layout; 
                    tmp.children.forEach((m) => {
                        let newPather01 = m.path.split("/")
                        let component01 = m.component
                        tmp.path = '/' + m.path.split("/")[0]
                        m.component = (resolve) => require([`@/views${component01}`],resolve)
                        m.name = '/' + newPather01[0] + '/' + newPather01[1]
                        m.path = newPather01[1]
                        if(m.res_isvalid == true){
                            // m.hidden == true
                        }
                    })
                    // console.log(tmp)
                } else {
                    let newPather = tmp.path.split("/")
                    tmp.path = '/' + newPather[0]
                    console.log(tmp.path, tmp.meta.title)
                    tmp.children.forEach((m) => {
                        m.name = '/' + newPather[0] + '/' + newPather[1]
                        m.path = newPather[1]
                        m.component = (resolve) => require([`@/views${m.component}`],resolve)
                    })
                }   
            }
            //判断是否有下级
            // if (tmp.children) {
            //     console.log(tmp.children.length)
            //     console.log(tmp.meta.title)
            //     tmp.children = filterAsyncRoutes(tmp.children, roles)
            // }
            res.push(tmp)
        }
    }) 
    // console.log(res, '获取的路由')
    return res
    
}

const state = {
    routes: [],
    addRoutes: [],
    oneRun:'true'
}

const mutations = {
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes
        state.routes = constantRoutes.concat(routes);
        // console.log(state.routes, 'state.routes')
        localStorage.setItem('routes', JSON.stringify(state.routes))
    },
    SET_ONERUN:(state,oneRun)=>{
        state.oneRun = oneRun
    }
}

const actions = {
    generateRoutes({
        commit,
        state
    }, roles) {
        return new Promise(resolve => {
            getResTree01().then((res) => {
                // console.log(res.data, '获取的动态路由')
                let accessedRoutes = filterAsyncRoutes(res.data, roles);
            // let accessedRoutes = filterAsyncRoutes(resOption.data, roles);
            // console.log(accessedRoutes, 'accessedRoutes')
            accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
            commit('SET_ROUTES', accessedRoutes)
            // resolve(state.routes)
            resolve(accessedRoutes)
            })
            // let accessedRoutes = filterAsyncRoutes(asyncRoutes, roles);
            // // let accessedRoutes = filterAsyncRoutes(res.data, roles);
            // // let accessedRoutes = filterAsyncRoutes(resOption.data, roles);
            // console.log(accessedRoutes, 'accessedRoutes')
            // commit('SET_ROUTES', accessedRoutes)
            // // resolve(state.routes)
            // resolve(accessedRoutes)
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
